//
//  MBSpaceMyCollectionShowViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceParentChildShowClassViewController.h"
#import "MBSpaceAddMusicViewController.h"
#import "MBSongDetailController.h"
#import "MBDetailViewController.h"
#import "MBSpaceAlbumViewController.h"

#import "MBSpaceCollectionCompileView.h"
#import "SharePLayerView.h"
#import "MBSpaceMyParentChildShowCollectionCell.h"
#import "MBSongDetailForIController.h"

#import "MBDetailViewForIpadController.h"
#import "MBSongDetailForIpadController.h"
@interface MBSpaceParentChildShowClassViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceCollectionCompileViewDelegate,SharePLayerViewDelegate,MBSpaceMyParentChildShowCollectionCellDelegate> {
    UITableView                             *_tableView;
    MBSpaceCollectionCompileView            *_compileView;
    NSMutableArray                          *_selectMusicArr;//保存选中的歌曲
    CLShareManager *shareView;
}

@property (nonatomic,assign)BOOL isCompiling;//是否批量编译
@property (nonatomic,assign)BOOL isAllSelcting;//是否全选

@end

@implementation MBSpaceParentChildShowClassViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self onUpdateUIForView:self.orientation];
    
    [self onLoadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isCompiling = NO;
    self.isAllSelcting = NO;
    _selectMusicArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    [self setNavBar];
    [self onCreateUI];
}

#pragma mark - Actions
#pragma mark - UI
- (void)setNavBar
{
    [self setTitleView:@"亲子秀"];
    [self setBackItem];
    
   [self setRightPlayerItem];
}

- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = [self setTableHeaderViewWithIndex:2];
    
    //批量弹出框
    _compileView = [[MBSpaceCollectionCompileView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100*PERCEN)];
    _compileView.delegate = self;
    _compileView.orientation = self.orientation;
    _compileView.hidden = YES;
    [self.view addSubview:_compileView];
    shareView = [[CLShareManager alloc] init];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _tableView.tableHeaderView = [self setTableHeaderViewWithIndex:3];
    _compileView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100*PERCEN);
    _compileView.orientation = self.orientation;
    
    NSArray *arr = [[NSUserDefaults standardUserDefaults]objectForKey:@"SELECT_MUSIC_QINZIXIU_ALL"];
    if ([arr isKindOfClass:[NSArray class]]) {
        if (arr.count > 0) {
            //弹出批量框
            _tableView.height = SCREEN_HEIGHT-_compileView.height;
            _compileView.top = _tableView.bottom;
            _compileView.hidden = NO;
        } else {
            //隐藏
            _tableView.height = SCREEN_HEIGHT;
            _compileView.top = _tableView.bottom;
            _compileView.hidden = YES;
            self.isAllSelcting = NO; //不全选
            [_selectMusicArr removeAllObjects]; //删除所有选中的歌曲
        }
    }
    
    [_tableView reloadData];
}

- (UIView *)setTableHeaderViewWithIndex:(NSInteger)index
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120*PERCEN)];
    bgView.backgroundColor = [UIColor clearColor];
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bgView.width, 100*PERCEN)];
    contentView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:contentView];
    
    float itemWidth = 20*PERCEN;
    float btWidth = 1.*(bgView.width-(index+1)*itemWidth)/index;
    if (ISIPAD) {
        itemWidth = 44*PERCEN;
        btWidth = 540*PERCEN;
    }
    for (int i=0; i<index; i++) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemWidth+(btWidth+itemWidth)*i, 18*PERCEN, btWidth, 64*PERCEN);
        if (ISIPAD) {
            bt.left = 440*PERCEN+(btWidth+itemWidth)*i;
        }
        bt.backgroundColor = [UIColor whiteColor];
        [bt setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        bt.layer.cornerRadius = 6.f;
        bt.layer.masksToBounds = YES;
        bt.layer.borderColor = UIColorFromHex(0x999999).CGColor;
        bt.layer.borderWidth = 1;
        bt.tag = 1000+i;
        bt.titleLabel.font = [UIFont systemFontOfSize:13.];
        [bt addTarget:self action:@selector(onTapForMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:bt];
        
        if (i == 0) {
            [bt setTitle:@"播放全部" forState:UIControlStateNormal];
        }
        else if (i == 1) {
            [bt setTitle:@"批量操作" forState:UIControlStateNormal];
        }
        else if (i == 2) {
            [bt setTitle:@"更多" forState:UIControlStateNormal];
        }
    }
    
    return bgView;
}

#pragma mark -加载数据
- (void)onLoadData
{
    if ([UserManager standerDefult].collectionMusicTypeArr.count == 0) return;
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    [arr addObjectsFromArray:[[UserManager standerDefult].collectionMusicTypeArr lastObject]];

     [_dataArr removeAllObjects];

     [_dataArr addObjectsFromArray:arr];
    [_tableView reloadData];
}


#pragma mark - 跳转播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

#pragma mark - 播放全部、批量、更多
- (void)onTapForMusicAction:(UIButton *)bt
{
    switch (bt.tag-1000) {
        case 0:
        {
            //播放全部
        }
            break;
        case 1:
        {
            //批量
            self.isCompiling = !self.isCompiling;
            if (self.isCompiling) {
                //弹出批量框
                _tableView.height = self.view.height-_compileView.height;
                _compileView.top = _tableView.bottom;
                _compileView.hidden = NO;
            } else {
                //隐藏
                _tableView.height = self.view.height;
                _compileView.top = _tableView.bottom;
                _compileView.hidden = YES;
                self.isAllSelcting = NO; //不全选
                [_selectMusicArr removeAllObjects]; //删除所有选中的歌曲
                
                [[NSUserDefaults standardUserDefaults]setObject:@[] forKey:@"SELECT_MUSIC_QINZIXIU_ALL"];
                [[NSUserDefaults standardUserDefaults]synchronize];
            }
            //返回按钮改变
            [self onChangeBackButton:self.isCompiling];
            
            [_tableView reloadData];
        }
            break;
        case 2:
        {
            //更多
            //1.批量还原
            if (self.isCompiling) {
                UIButton *bt = (UIButton *)[self.view viewWithTag:1001];
                [self onTapForMusicAction:bt];
            }
            //2,显示
            SharePLayerView *show = [[SharePLayerView alloc]initWithFrame:CGRectZero withType:ShowPlayerType_Moer];
            show.delegate = self;
            show.orientation = self.orientation;
            [show onShowInWindow];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 改变返回按钮样式
- (void)onChangeBackButton:(BOOL) compileStatus {
    if (compileStatus) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 60, 30);
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14.]];
        [btn setTitle:@"全选" forState:UIControlStateNormal];
        [btn setTitle:@"全不选" forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 20)];
        [btn addTarget:self action:@selector(onSelectTapAllMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self setLeftBarItem:btn];
        [self setRightBarItems:nil];
    } else {
        [self setBackItem];
        
        [self setRightPlayerItem];
    }
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 批量操作 取消、删除、收藏
- (void)onSelectItemForCancel:(MBSpaceCollectionCompileView *)view
{
    UIButton *bt = (UIButton *)[self.view viewWithTag:1001];
    [self onTapForMusicAction:bt];
}
- (void)onSelectItemForDelete:(MBSpaceCollectionCompileView *)view
{
}
- (void)onSelectItemForNoCollection:(MBSpaceCollectionCompileView *)view
{
    
}

#pragma mark - 勾选一首歌曲
- (void)onSelectPCSItemForGouXuanWithValue:(id)value WithSelect:(BOOL)isSelect WithView:(MBSpaceMyParentChildShowCollectionCell *)view
{
    //数据保存
    NSLog(@"数据是否需要保存:%d",isSelect);
    if (isSelect) {
        //保存
        [_selectMusicArr addObject:value];
    } else {
        //删除
        [_selectMusicArr removeLastObject];
    }
    
    [[NSUserDefaults standardUserDefaults]setObject:_selectMusicArr forKey:@"SELECT_MUSIC_QINZIXIU_ALL"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    //删除、收藏是否可选中
    [_compileView onUpdateDataForDelegateAndCollection:(_selectMusicArr.count > 0 ? YES : NO)];
    
}


#pragma mark - 全选
- (void)onSelectAllMusicAction:(BOOL)isAllSelect
{
    self.isAllSelcting = isAllSelect;
    
    //测试
    [_selectMusicArr removeAllObjects];
    if (isAllSelect) {
        [_selectMusicArr addObject:@"1"];
        [_selectMusicArr addObject:@"1"];
        [_selectMusicArr addObject:@"1"];
    }
    
    [[NSUserDefaults standardUserDefaults]setObject:_selectMusicArr forKey:@"SELECT_MUSIC_QINZIXIU_ALL"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    //删除、收藏是否可选中
    [_compileView onUpdateDataForDelegateAndCollection:(_selectMusicArr.count > 0 ? YES : NO)];
    
    [_tableView reloadData];
}

//全选
- (void)onSelectTapAllMusicAction:(UIButton *)bt
{
    bt.selected = !bt.selected;
    if (bt.selected) {
        [bt setTitle:@"全不选" forState:UIControlStateNormal];
        [bt setTitle:@"全不选" forState:UIControlStateHighlighted];
        [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 15)];
    } else {
        [bt setTitle:@"全选" forState:UIControlStateNormal];
        [bt setTitle:@"全选" forState:UIControlStateHighlighted];
        [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
    }
    [self onSelectAllMusicAction:bt.selected];
}

#pragma mark - 更多操作 添加歌曲、播放、分享、取消收藏、下载
//添加歌曲
- (void)onSelctItemForMoreAddMusic:(SharePLayerView *)view
{
    MBSpaceAddMusicViewController *addMusic = [[MBSpaceAddMusicViewController alloc]init];
    addMusic.type = MBSpaceAddMusicType_SetAlbum;
    addMusic.dataDic = _dataDic;
    [self.navigationController pushViewController:addMusic animated:YES];
}

//播放
- (void)onSelctItemForMorePlayMusic:(SharePLayerView *)view
{
    if (ISIPAD) {
        MBSongDetailForIpadController*song=[[MBSongDetailForIpadController alloc]init];
        [self.navigationController pushViewController:song animated:YES];
        return;
    }
    
    MBSongDetailController *song = [[MBSongDetailController alloc]init];
    [self.navigationController pushViewController:song animated:YES];
}

//分享
- (void)onSelctItemForMoreShareMusic:(SharePLayerView *)view 
{
    [shareView setShareVC:self content:@"分享" image:[UIImage imageNamed:@"test"] url:@"https://www.baidu.com"];
    [shareView show];
    
}
//取消收藏
- (void)onSelctItemForMoreCancelMusicCollection:(SharePLayerView *)view
{
    NSLog(@"取消收藏音乐");
}

//下载
- (void)onSelctItemForMoreDownMusic:(SharePLayerView *)view
{
    NSLog(@"取消收藏音乐");
}

//亲子秀的更多操作
- (void)onSelectPCSItemForMoewWithValue:(id)value withView:(MBSpaceMyParentChildShowCollectionCell *)view
{
    SharePLayerView *show = [[SharePLayerView alloc]initWithFrame:CGRectZero withType:ShowPlayerType_MusicMoew];
    show.delegate = self;
    show.orientation = self.orientation;
    [show onShowInWindow];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //亲子秀
    static NSString *cellID = @"Collection_ParentChildShow_Cell";
    MBSpaceMyParentChildShowCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceMyParentChildShowCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
    }
    [cell onCompileCellForLayout:self.isCompiling WithAllSelct:self.isAllSelcting];
    
    if (_dataArr.count > indexPath.row) {
        cell.dataDic = [_dataArr objectAtIndex:indexPath.row];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190*PERCEN;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"*******");
    
    if (!self.isCompiling) {
//        if (ISIPAD) {
//            MBSongDetailForIController *childShowVC = [[MBSongDetailForIController alloc] init];
//            childShowVC.type=IdentityType_son;
//            [self.navigationController pushViewController:childShowVC animated:YES];
//
 //   }
        if (ISIPAD) {
            MBDetailViewForIpadController*childShowVC=[[MBDetailViewForIpadController alloc]init];
         
            [self.navigationController pushViewController:childShowVC animated:YES];
            return;
            
        }
        else {
        //亲子秀
        MBDetailViewController *song = [[MBDetailViewController alloc]init];
        [self.navigationController pushViewController:song animated:YES];
        }
    }
    
    
    
}

@end
